package com.arpa.ntocc.common.common.validator.aspect;

import com.arpa.ntocc.common.common.validator.annotation.PartyGroupCodeExistConstraint;
import com.arpa.ntocc.common.domain.entity.PartyGroup;
import com.arpa.ntocc.common.mapper.PartyGroupMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author liuxr
 * @date 2021年11月12日 15:33
 */
@Component
public class PartyGroupCodeExistValidator implements ConstraintValidator<PartyGroupCodeExistConstraint,String> {

    @Resource
    private PartyGroupMapper partyGroupMapper;


    /**
     * Implements the validation logic.
     * The state of {@code value} must not be altered.
     * <p>
     * This method can be accessed concurrently, thread-safety must be ensured
     * by the implementation.
     *
     * @param value   object to validate
     * @param context context in which the constraint is evaluated
     * @return {@code false} if {@code value} does not pass the constraint
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        int count = partyGroupMapper.selectCount(Wrappers.lambdaQuery(PartyGroup.class).eq(PartyGroup::getCode,value));
        if(count>0) {
            return false;
        }else{
            return true;
        }
    }
}
